package com.winforsys.yellowbook;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MyMapViewOverlay extends Overlay 
{
	Context context;
	GeoPoint point;
	Resources resources;
	
	public MyMapViewOverlay(Context context, GeoPoint pt, Resources res) {
		
		this.context = context;
		
		if (pt != null) {
			this.point = new GeoPoint(pt.getLatitudeE6(), pt.getLongitudeE6());
		}
		else {
			this.point = null;
		}
		
		this.resources = res;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapView, shadow);
	
		if (point != null) {
		
			Paint paint = new Paint();
			
			
			paint.setAntiAlias(true);
			paint.setTextSize(30);
			
			Projection projection = mapView.getProjection();
			Point coord = projection.toPixels(this.point, null);
			
			Bitmap bmp = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher);
			
			canvas.drawBitmap(bmp, coord.x, coord.y, paint);
		}
	}
	
	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		// TODO Auto-generated method stub
		String message = String.format("X = %s, Y = %s", p.getLatitudeE6(), p.getLongitudeE6());
		
		Toast.makeText(this.context, message, Toast.LENGTH_SHORT).show();
		
		return true;
	}
}
